package com.ttt.controller;

import cn.hutool.core.util.ObjectUtil;
import com.ttt.entity.Result;
import com.ttt.entity.Userinfo;
import com.ttt.service.LoginService;
import com.ttt.service.MenuService;
import com.ttt.utli.Mikepasswordsalt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author ：StudentLsj（李世界）
 * @date ：Created in 2022/8/29 19:18
 * @description：
 * @modified By：
 * @version:
 */
@Controller
@RequestMapping("/user")
public class LoginController extends BaseController{
    @Autowired
    LoginService loginService;
    @Autowired
    MenuService menuService;


    @RequestMapping("/login.do")
    @ResponseBody
    public Result login(Userinfo userinfo, HttpSession session){
        String username=userinfo.getUsername();
        String password=userinfo.getPassword();
        Userinfo newuser = loginService.login(username);
        String salt = newuser.getSalt();
        String s = Mikepasswordsalt.makePasswordSalt(password, salt);
        if (ObjectUtil.isEmpty(newuser)){
            return fail("用户名不存在");
        }else{
            if (s.equals(newuser.getPassword())){
                Map<String, List> listMap = menuService.queryAll(newuser.getUserid());
                session.setAttribute("menu",listMap);
                session.setAttribute("newuser",newuser);
               return success();
            }else {
                return fail("密码错误");
            }
        }
    }
}
